package com.xuqm.sdhbwfu.app.ui

import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.bigkoo.alertview.AlertView
import com.xuqm.base.adapter.CommonAdapter
import com.xuqm.base.adapter.ViewHolder
import com.xuqm.base.common.AppManager
import com.xuqm.base.common.ToolsHelper
import com.xuqm.base.extensions.getStringForPreferences
import com.xuqm.base.extensions.putString
import com.xuqm.base.ui.BaseActivity
import com.xuqm.sdhbwfu.app.R
import com.xuqm.sdhbwfu.app.databinding.ActivityMainBinding
import com.xuqm.sdhbwfu.app.model.MainListModel
import com.xuqm.sdhbwfu.app.viewmodel.MainViewModel
import com.xuqm.sdhbwfu.core.common.SHARE_UESR_NAME
import com.xuqm.sdhbwfu.core.common.SHARE_UESR_PASSWORD


class MainActivity : BaseActivity<ActivityMainBinding>() {
    override fun getLayoutId(): Int =
        R.layout.activity_main

    override fun fullscreen(): Boolean = true

    val viewModel: MainViewModel by viewModels()

    override fun initData() {
        super.initData()
        viewModel.locationList()
        viewModel.pureList()
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        binding.mainListRv.layoutManager = GridLayoutManager(mContext, 4)
        binding.mainListRv.adapter = adapter

        adapter.setmDatas(
            listOf(
                MainListModel("移动巡检", R.drawable.icon_main_inspection),
                MainListModel("当前值班信息", R.drawable.icon_main_job),
                MainListModel("风险单", R.drawable.icon_main_order),
                MainListModel("实施", R.drawable.icon_main_order),
                MainListModel("应急演练", R.drawable.icon_main_order)
            )
        )

        adapter.setItemClickListener { _, _, position ->
            when (position) {
                0 -> startActivity(Intent(mContext, InspectionActivity::class.java))
                1 -> startActivity(Intent(mContext, JobListActivity::class.java))
                2 -> startActivity(Intent(mContext, RiskListActivity::class.java))
                3 -> startActivity(Intent(mContext, PmTaskListActivity::class.java))
                4 -> startActivity(Intent(mContext, YLEnterActivity::class.java))
            }
        }

        binding.mainUserTv.text = "当前用户：${getStringForPreferences(SHARE_UESR_NAME)}"

        binding.mainLogoutTv.setOnClickListener {
            AlertView(
                "退出",
                "退出会清除已保存的密码",
                "",
                arrayOf("取消", "确定"),
                null,
                AppManager.getInstance().activity,
                AlertView.Style.Alert
            ) { _, pos ->
                when (pos) {
                    0 -> {
                    }
                    else -> {
                        putString(SHARE_UESR_PASSWORD, "")
                        AppManager.getInstance().exit()
                    }
                }

            }.show()
        }

    }

    private val adapter = object : CommonAdapter<MainListModel>(R.layout.item_app_list) {
        override fun convert(holder: ViewHolder, item: MainListModel, position: Int) {
            holder.setText(R.id.app_name, item.name)
            holder.setImageResource(R.id.app_icon, item.res)
        }

    }


    private var oldTime = 0L
    override fun onBackPressed() {
        val newTime = System.currentTimeMillis()
        if (newTime - oldTime < 1500 && oldTime != 0L)
            AppManager.getInstance().exit()
        else {
            oldTime = newTime
            ToolsHelper.showMessage("双击退出")
        }
    }


}